<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="40px" class="formClass">
      <el-form-item label="地址" prop="petAddress">
        <el-input
          v-model="queryParams.petAddress"
          placeholder="请输入地址"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来源" prop="petSource">
        <el-input
          v-model="queryParams.petSource"
          placeholder="请输入来源"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品种" prop="petKind">
        <treeselect v-model="queryParams.petKind"
                    style="width: 200px"
                    :defaultExpandLevel=1
                    :options="deptOptions"
                    :show-count="true"
                    placeholder="请选择宠物品种" />
      </el-form-item>
      <el-form-item label="价格" prop="petPrice">
        <el-input
          v-model="queryParams.petPrice"
          placeholder="请输入可接受的最高价格"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" v-if="isExpandAll" prop="petName">
        <el-input
          v-model="queryParams.petName"
          placeholder="请输入宠物昵称"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" v-if="isExpandAll" prop="petSex">
        <el-select v-model="queryParams.petSex" style="width: 200px" placeholder="请选择性别" clearable size="small">
          <el-option
            v-for="dict in dict.type.animal_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="年龄" v-if="isExpandAll" prop="petAge">
        <el-input
          v-model="queryParams.petAge"
          placeholder="请输入年龄信息"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性格" v-if="isExpandAll" prop="petCharacter">
        <el-input
          v-model="queryParams.petCharacter"
          placeholder="请输入性格"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="健康状况" v-if="isExpandAll" label-width="68px" prop="petHealth">
        <el-input
          v-model="queryParams.petHealth"
          placeholder="请输入健康状况"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建日期" v-if="isExpandAll" label-width="68px" prop="createTime">
        <el-date-picker clearable
                        v-model="queryParams.createTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item style="display:block;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="searchAll"
        >更多
        </el-button>
      </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['adopt:pet:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['adopt:pet:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['adopt:pet:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['adopt:pet:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" @change="changeHeight"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="petList" :height="tableHeight" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="宠物昵称" :show-overflow-tooltip="true" min-width="110px" align="center" prop="petName" />
      <el-table-column label="性别" align="center" prop="petSex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.animal_sex" :value="scope.row.petSex"/>
        </template>
      </el-table-column>
      <el-table-column label="年龄" :show-overflow-tooltip="true" min-width="110px" align="center" prop="petAge" />
      <el-table-column label="品种" :show-overflow-tooltip="true" min-width="110px" align="center" prop="deptName" />
      <el-table-column label="照片" align="center" prop="petPhoto" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.petPhoto" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="地址" :show-overflow-tooltip="true" min-width="110px" align="center" prop="petAddress" />
      <el-table-column label="联系方式"  min-width="200px" align="center" prop="petContact" :show-overflow-tooltip="true"/>
      <el-table-column label="价格" :show-overflow-tooltip="true" min-width="110px" align="center" prop="petPrice" />
      <el-table-column label="来源" :show-overflow-tooltip="true" min-width="200px" align="center" prop="petSource" />
      <el-table-column label="毛色" :show-overflow-tooltip="true" min-width="110px" align="center" prop="hairColor" />
      <el-table-column label="健康状况" :show-overflow-tooltip="true" min-width="110px" align="center" prop="petHealth" />
      <el-table-column label="免疫情况" :show-overflow-tooltip="true" min-width="110px" align="center" prop="immuneCondition" />
      <el-table-column label="绝育情况" :show-overflow-tooltip="true" min-width="110px" align="center" prop="sterilizationSituation" />
      <el-table-column label="性格" :show-overflow-tooltip="true" min-width="110px" align="center" prop="petCharacter" />
      <el-table-column label="补充信息" :show-overflow-tooltip="true" min-width="110px" align="center" prop="petSupplement" />
      <el-table-column label="创建人" :show-overflow-tooltip="true" min-width="110px" align="center" prop="createUserName"/>
      <el-table-column label="创建日期" min-width="200px" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
     <el-table-column label="操作" align="center" min-width="130px" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['adopt:pet:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['adopt:pet:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改宠物对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" class="dialog_slide max-width-limit" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="petForm">
        <el-row>
          <el-col :span="8">
            <el-form-item label="宠物昵称" prop="petName">
              <el-input v-model="form.petName" placeholder="请输入宠物昵称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="年龄" prop="petAge">
              <el-input v-model="form.petAge" placeholder="请输入年龄信息" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="性别" prop="petSex">
              <el-select v-model="form.petSex" placeholder="请选择性别">
                <el-option
                  v-for="dict in dict.type.animal_sex"
                  :key="dict.value"
                  :label="dict.value"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="品种" prop="petKind">
              <treeselect v-model="form.petKind" :options="deptOptions" :defaultExpandLevel=1 :show-count="true" placeholder="请选择宠物品种" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="价格" prop="petPrice">
              <el-input v-model="form.petPrice" placeholder="请输入价格" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="性格" prop="petCharacter">
              <el-input v-model="form.petCharacter" placeholder="请输入性格" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="毛色" prop="petAge">
              <el-input v-model="form.hairColor" placeholder="请输入毛色信息" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="来源" prop="petSource">
              <el-input v-model="form.petSource" placeholder="请输入来源信息" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="免疫情况" prop="immuneCondition">
              <el-input v-model="form.immuneCondition" placeholder="请输入宠物免疫情况，是否已接种疫苗等" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="绝育情况" prop="sterilizationSituation">
              <el-input v-model="form.sterilizationSituation" placeholder="请输入绝育情况，是否已绝育等" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="健康状况" prop="petHealth">
              <el-input v-model="form.petHealth" placeholder="请输入宠物健康状况" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式" prop="petContact">
              <el-input v-model="form.petContact" placeholder="请输入联系方式" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="补充信息" prop="petSupplement">
          <el-input v-model="form.petSupplement" type="textarea" placeholder="请输入补充信息，可以是宠物的其他信息、领养要求等。尽可能详细" />
        </el-form-item>
        <el-form-item label="地址" prop="petAddress">
          <el-input v-model="form.petAddress" type="textarea" placeholder="请输入地址，尽可能详细" />
        </el-form-item>
        <el-form-item label="照片" prop="petPhoto">
          <image-upload v-model="form.petPhoto"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-bottom: 20px;margin-right: 20px">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPet, getPet, delPet, addPet, updatePet } from "@/api/adopt/pet";
import {treeselect} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "ForAdopt",
  components: { Treeselect },
  dicts: ['sys_yes_no', 'animal_sex'],
  data() {
    return {
      // 部门树选项
      deptOptions: [],
      //table高度自适应
      tableHeight: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //默认不显示
      isExpandAll: false,
      // 总条数
      total: 0,
      // 宠物表格数据
      petList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        createUserId:this.$store.state.user.id,
        pageNum: 1,
        pageSize: 12,
        petAddress: null,
        petSource: null,
        petKind: null,
        petPrice: null,
        petName: null,
        petSex: null,
        petAge: null,
        petNumber: null,
        petHealth:null,
        petCharacter: null,
        createTime:null,
        petSupplement: null,
        isDelete:"0",
        isAdopted:"0",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        petKind:{required:true,trigger:'blur',message:"请选择宠物品种"},
        petPhoto:{required:true,trigger:'blur',message:"请上传宠物照片"},
        petAddress:{required:true,trigger:'blur',message:"请填写地址"},
        petContact:{required:true,trigger:'blur',message:"请输入联系方式"},
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  // 表格高度自适应
  mounted () {
    this.$nextTick(function () {
      this.getHeight();
    })
    window.addEventListener('resize',this.getHeight,false)
  },
  methods: {
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 展开/折叠操作 */
    searchAll() {
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(function () {
        this.getHeight();
      })
    },
    changeHeight(){
      this.$nextTick(function () {
        this.getHeight();
      })
    },
    getHeight(){
      const _this=this;
      let height1=document.defaultView.getComputedStyle(_this.$el, null)["padding-top"];
      height1=height1.replace("px","");
      let height2=document.getElementsByClassName("el-table")[0].offsetTop+80;
      _this.tableHeight=document.documentElement.clientHeight-height2-height1-60;
    },
    /** 查询宠物列表 */
    getList() {
      this.loading = true;
      listPet(this.queryParams).then(response => {
        this.petList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        petId: null,
        petName: null,
        petSex: null,
        petAge: null,
        petCharacter: null,
        petKind: null,
        petSupplement: null,
        petSell: "0",
        petPrice: null,
        petPhoto: null,
        petAddress: null,
        petContact: null,
        petSource: null,
        petNumber: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.petId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加宠物信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const petId = row.petId || this.ids
      getPet(petId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改宠物信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.petId != null) {
            updatePet(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPet(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const petIds = row.petId || this.ids;
      this.$modal.confirm('是否确认删除宠物编号为"' + petIds + '"的数据项？').then(function() {
        return delPet(petIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('adopt/pet/export', {
        ...this.queryParams
      }, `pet_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.petForm{
  margin-right: 30px;
  margin-left: -10px;
  margin-top: -10px;
  margin-bottom: -40px;
  ::v-deep{
    .el-form-item{
      margin-bottom: 20px;
    }
  }
}
</style>
